﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GestionTaxesRiaApp.Web;
using System.ServiceModel.DomainServices.Client;

namespace GestionTaxesRiaApp.Views
{
    public partial class AjouterFrequentation : Page
    {
        GestionTaxesContext gestionTaxesContext = new GestionTaxesContext();
        public float mtTarif;
        Frequentation frequentation = new Frequentation();
        int nbNuitTarifReduit = 0;
        int nbNuitTarifPlein = 0;
        float mtPourcentage = 0;

        public AjouterFrequentation()
        {
            SetDroits();
            InitializeComponent();
        }

        // S'exécute lorsque l'utilisateur navigue vers cette page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        void LogementLoadedCallback(LoadOperation<Logement> loadOperation)
        {
            log_NomComboBox.DataContext = loadOperation.Entities;
        }

        private void frequentationDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void reductionDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void logementDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            
            mtTarif = 100;

            if (Connexion.utilisateurStatic.uti_IdFonction == Home.hebergeurInt)
            {
                frequentation.fre_IdProprietaire = Connexion.utilisateurStatic.uti_Id;
            }
            else
            {
                frequentation.fre_IdProprietaire = Int32.Parse(log_IdProprietaireTextBox.Text);
            }
           
            frequentation.fre_IdLogement = Int32.Parse(log_IdTextBox1.Text);
            frequentation.fre_NbNuitTarifPlein = Int32.Parse(PleinTarifTb.Text);
            frequentation.fre_IdReduction = Int32.Parse(red_IdTextBox4.Text);

            var tarif  = gestionTaxesContext.Load<Tarif>(
            gestionTaxesContext.GetTarifByIdQuery(Int32.Parse(log_IdTarifTextBox.Text)),
            tarifLoadedCallback, false);

            
        }

        void tarifLoadedCallback(LoadOperation<Tarif> loadOperation)
        {
            var tarif = loadOperation.Entities.FirstOrDefault();
            if (tarif != null)
            {
                mtTarif = tarif.tar_Montant;
            }
            if (TarifReduitTb.Text.Equals("") || Int32.Parse(TarifReduitTb.Text) == 0)
            {
                frequentation.fre_NbNuitTarifReduit = 0;
                frequentation.fre_NbNuitTotal = Int32.Parse(PleinTarifTb.Text);
                frequentation.fre_Montant = frequentation.fre_NbNuitTotal * mtTarif;
            }
            else
            {
                nbNuitTarifReduit = Int32.Parse(TarifReduitTb.Text);
                nbNuitTarifPlein = Int32.Parse(PleinTarifTb.Text);
                mtPourcentage = Single.Parse(red_PourcentageTextBox.Text);
                frequentation.fre_NbNuitTarifReduit = Int32.Parse(TarifReduitTb.Text);
                frequentation.fre_NbNuitTotal = Int32.Parse(PleinTarifTb.Text) + Int32.Parse(TarifReduitTb.Text);
                frequentation.fre_Montant = (nbNuitTarifPlein + (nbNuitTarifReduit - ((nbNuitTarifReduit * mtPourcentage) / 100))) * mtTarif;

            }
            frequentation.fre_Date = System.DateTime.Now;
            frequentation.fre_IsValider = 0;

            gestionTaxesContext.Frequentations.Add(frequentation);
            gestionTaxesContext.SubmitChanges();
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/GestionFrequentation", UriKind.Relative));  
        }

        private void proprietaireDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void tarifDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/GestionFrequentation", UriKind.Relative));
        }
        private void SetDroits()
        {
            if (Connexion.utilisateurStatic.uti_IdFonction != Home.administrateurInt && Connexion.utilisateurStatic.uti_IdFonction != Home.hebergeurInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Gestions", UriKind.Relative));
            }
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.decoInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Connexion", UriKind.Relative));
            }
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.hebergeurInt)
            {
               var logements = gestionTaxesContext.Load<Logement>(gestionTaxesContext.GetLogementsByProprietaireIdQuery(Connexion.utilisateurStatic.uti_Id),LogementLoadedCallback,false);
            }
        }

    }
}
